# Flores, Alejandro and Alexander Coppock
# Do Bilinguals Respond More Favorably to Candidate Advertisements in English or in Spanish?
# Forthcoming, Political Communication

rm(list = ls())

# Set your working directory to the replication archive directory

# Install software if necessary
# install.packages(c("tidyverse","coefplot", "estimatr"))

library(tidyverse)
library(coefplot)
library(estimatr)

study_2 <- read_csv("study_2.csv")
study_3 <- read_csv("study_3.csv")

study_2 <- study_2 %>% mutate(democrat = as.numeric(pid_7 %in% c(1, 2, 3)),
                              republican = as.numeric(pid_7 %in% c(5, 6, 7)))
study_3 <- study_3 %>% mutate(democrat = as.numeric(pid_7 %in% c(1, 2, 3)),
                              republican = as.numeric(pid_7 %in% c(5, 6, 7)))

fit_1_b_d <- lm_robust(vela_general ~ Z_vela, data = filter(study_2, bilingual == 1, democrat == 1))
fit_1_m_d <- lm_robust(vela_general ~ Z_vela, data = filter(study_3, bilingual == 0, democrat == 1))
fit_2_b_d <- lm_robust(coffman_general ~ Z_coffman, data = filter(study_2, bilingual == 1, democrat == 1))
fit_2_m_d <- lm_robust(coffman_general ~ Z_coffman, data = filter(study_3, bilingual == 0, democrat == 1))
fit_3_b_d <- lm_robust(like_vela ~ Z_vela, data = filter(study_2, bilingual == 1, democrat == 1))
fit_3_m_d <- lm_robust(like_vela ~ Z_vela, data = filter(study_3, bilingual == 0, democrat == 1))
fit_4_b_d <- lm_robust(like_coffman ~ Z_coffman, data = filter(study_2, bilingual == 1, democrat == 1))
fit_4_m_d <- lm_robust(like_coffman ~ Z_coffman, data = filter(study_3, bilingual == 0, democrat == 1))
fit_6_b_d <- lm_robust(vela_cares ~ Z_vela, data = filter(study_2, bilingual == 1, democrat == 1))
fit_6_m_d <- lm_robust(vela_cares ~ Z_vela, data = filter(study_3, bilingual == 0, democrat == 1))
fit_7_b_d <- lm_robust(coffman_cares ~ Z_coffman, data = filter(study_2, bilingual == 1, democrat == 1))
fit_7_m_d <- lm_robust(coffman_cares ~ Z_coffman, data = filter(study_3, bilingual == 0, democrat == 1))
fit_9_b_d <- lm_robust(conf_in_vela ~ Z_vela, data = filter(study_2, bilingual == 1, democrat == 1))
fit_9_m_d <- lm_robust(conf_in_vela ~ Z_vela, data = filter(study_3, bilingual == 0, democrat == 1))
fit_10_b_d <- lm_robust(conf_in_coffman ~ Z_coffman, data = filter(study_2, bilingual == 1, democrat == 1))
fit_10_m_d <- lm_robust(conf_in_coffman ~ Z_coffman, data = filter(study_3, bilingual == 0, democrat == 1))

fit_1_b_r <- lm_robust(vela_general ~ Z_vela, data = filter(study_2, bilingual == 1, republican == 1))
fit_1_m_r <- lm_robust(vela_general ~ Z_vela, data = filter(study_3, bilingual == 0, republican == 1))
fit_2_b_r <- lm_robust(coffman_general ~ Z_coffman, data = filter(study_2, bilingual == 1, republican == 1))
fit_2_m_r <- lm_robust(coffman_general ~ Z_coffman, data = filter(study_3, bilingual == 0, republican == 1))
fit_3_b_r <- lm_robust(like_vela ~ Z_vela, data = filter(study_2, bilingual == 1, republican == 1))
fit_3_m_r <- lm_robust(like_vela ~ Z_vela, data = filter(study_3, bilingual == 0, republican == 1))
fit_4_b_r <- lm_robust(like_coffman ~ Z_coffman, data = filter(study_2, bilingual == 1, republican == 1))
fit_4_m_r <- lm_robust(like_coffman ~ Z_coffman, data = filter(study_3, bilingual == 0, republican == 1))
fit_6_b_r <- lm_robust(vela_cares ~ Z_vela, data = filter(study_2, bilingual == 1, republican == 1))
fit_6_m_r <- lm_robust(vela_cares ~ Z_vela, data = filter(study_3, bilingual == 0, republican == 1))
fit_7_b_r <- lm_robust(coffman_cares ~ Z_coffman, data = filter(study_2, bilingual == 1, republican == 1))
fit_7_m_r <- lm_robust(coffman_cares ~ Z_coffman, data = filter(study_3, bilingual == 0, republican == 1))
fit_9_b_r <- lm_robust(conf_in_vela ~ Z_vela, data = filter(study_2, bilingual == 1, republican == 1))
fit_9_m_r <- lm_robust(conf_in_vela ~ Z_vela, data = filter(study_3, bilingual == 0, republican == 1))
fit_10_b_r <- lm_robust(conf_in_coffman ~ Z_coffman, data = filter(study_2, bilingual == 1, republican == 1))
fit_10_m_r <- lm_robust(conf_in_coffman ~ Z_coffman, data = filter(study_3, bilingual == 0, republican == 1))

fit_list_b_d <- list(fit_1_b_d, fit_2_b_d, fit_3_b_d, fit_4_b_d, fit_6_b_d, fit_7_b_d, fit_9_b_d, fit_10_b_d)
fit_list_m_d <- list(fit_1_m_d, fit_2_m_d, fit_3_m_d, fit_4_m_d, fit_6_m_d, fit_7_m_d, fit_9_m_d, fit_10_m_d)
fit_list_b_r <- list(fit_1_b_r, fit_2_b_r, fit_3_b_r, fit_4_b_r, fit_6_b_r, fit_7_b_r, fit_9_b_r, fit_10_b_r)
fit_list_m_r <- list(fit_1_m_r, fit_2_m_r, fit_3_m_r, fit_4_m_r, fit_6_m_r, fit_7_m_r, fit_9_m_r, fit_10_m_r)


gg_df <- 
  bind_rows(
    (fit_list_b_d %>% map_df(tidy) %>% mutate(sample = "Bilingual Sample", party = "Democratic Respondents")),
    (fit_list_m_d %>% map_df(tidy) %>% mutate(sample = "Monolingual Sample", party = "Democratic Respondents")),
    (fit_list_b_r %>% map_df(tidy) %>% mutate(sample = "Bilingual Sample", party = "Republican Respondents")),
    (fit_list_m_r %>% map_df(tidy) %>% mutate(sample = "Monolingual Sample", party = "Republican Respondents"))) %>%
  filter(term != "(Intercept)") 

gg_df <- 
  within(gg_df,{
    dv_group = rep(NA, nrow(gg_df))
    dv_group[outcome %in% c("like_coffman", "like_vela", "like_bush")] <- "Like Candidate \n (1-7)"
    dv_group[outcome %in% c("conf_in_coffman", "conf_in_vela", "conf_in_bush")] <- "Confidence Re: Immigration \n (1-4)"
    dv_group[outcome %in% c("coffman_general", "vela_general", "bush_general")] <- "Support Candidate in General \n (0-1)"
    dv_group[outcome %in% c("coffman_cares", "vela_cares", "bush_cares")] <- "Candidate Cares \n (0-1)"
    dv_group <- factor(dv_group, levels = c("Support Candidate in General \n (0-1)", "Like Candidate \n (1-7)", "Candidate Cares \n (0-1)", "Confidence Re: Immigration \n (1-4)"))
    dv_group <- factor(dv_group, levels = rev(levels(dv_group)))
    
    candidate = rep(NA, nrow(gg_df))
    candidate[term == "Z_coffman"] <- "White Republican \n Cong. Candidate (Coffman)"
    candidate[term == "Z_vela"] <- "Latino Democratic \n Cong. Candidate (Vela)"
    candidate[term == "Z_adspanish_ad"] <- "White Republican \n Pres. Candidate (Bush)"
    candidate <- factor(candidate, levels = c("White Republican \n Pres. Candidate (Bush)",
                                              "Latino Democratic \n Cong. Candidate (Vela)",
                                              "White Republican \n Cong. Candidate (Coffman)"))
  })


g <- 
ggplot(gg_df, aes(x = estimate, y = dv_group, group = sample, color = sample, shape = sample)) +
  geom_point(size = 2, position = position_dodgev(height = -.5)) +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), position = position_dodgev(height = -.5), height = 0) +
  facet_grid(party~candidate) +
  scale_color_manual(values = c("red", "blue")) +
  geom_vline(xintercept = 0, linetype = "dashed") + 
  theme_bw() +
  xlab("Conditional Average Treatment Effect Estimates with 95% Confidence Intervals") +
  ggtitle(label = "Estimated Treatment Effects of Spanish versus English Language Advertisements", subtitle = "By Respondent Partisanship") +
  theme(legend.position = "bottom",
        legend.title = element_blank(),
        axis.title = element_blank(),
        strip.background = element_blank())

ggsave(filename = "flores_coppock_figure_2.pdf", g, width = 7, height = 5)
